/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: MaterialController
 * Author:   Administrator
 * Date:     2021/8/3 0003 18:56
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.hxlc.tie.demo.modular.material.controller;

import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.modular.material.model.param.MaterialParam;
import cn.hxlc.tie.demo.modular.material.model.result.MaterialResult;
import cn.hxlc.tie.demo.modular.material.service.MaterialService;
import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.libs.scanner.annotation.PostResource;
import cn.hxlc.tie.libs.scanner.stereotype.ApiResource;
import cn.hxlc.tie.model.page.PageResult;
import cn.hxlc.tie.model.web.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static cn.hxlc.tie.model.web.response.ResponseData.DEFAULT_SUCCESS_CODE;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2021/8/3 0003
 * @since 1.0.0
 */
@RestController
@ApiResource(name = "物品资料", path = "/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    @GetResource(name = "分页查询列表", path = "/queryListPage", requiredPermission = false)
    @ApiOperation(value = "分页查询列表", response = MaterialResult.class)
    public ResponseData queryListPage(MaterialParam param) {

        PageResult<MaterialResult> pageBySpec = materialService.findPageBySpec(param);
        return ResponseData.success(pageBySpec);
    }

    @GetResource(name = "查询详情", path = "/queryDetail", requiredPermission = false)
    @ApiOperation(value = "查询详情", response = MaterialResult.class)
    public ResponseData queryDetail(@RequestParam Long fid) {
        return ResponseData.success(materialService.detail(fid));
    }

    @PostResource(name = "添加", path = "/add")
    @ApiOperation("新增")
    public ResponseData add(@RequestBody MaterialParam param) {

        materialService.add(param);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"添加成功",null);
    }

    @PostResource(name = "批量操作", path = "/batchChangeState" ,requiredPermission = false)
    @ApiOperation(value = "批量禁用启用" )
    public ResponseData batchChangeState(@RequestBody Map params) {
        JSONArray jfids = (JSONArray)params.get("fid");
        Object []fid =jfids.toArray();
        Integer fstatus = (Integer)params.get("fstatus");
        materialService.batchChangeState(fid, StatusEnum.toEnum(fstatus));
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }

    @PostResource(name = "修改", path = "/update")
    @ApiOperation("修改")
    public ResponseData update(@RequestBody MaterialParam param) {
        materialService.update(param);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"更新成功",null);
    }

}
